
public class QueueBlock {
    private int[] array;
    private int head;
    private int last;
    private int  size;
    void put(int elem) throws InterruptedException {
        //判断是否满了，队列满进入阻塞状态
        //判满时需要加锁，保证数据真实有效
        synchronized (this) {
            while(array.length >= size) {
                wait();//阻塞等待
            }
        }
        array[last] = elem;
        last++;
        if(last >= array.length) {
            last = 0;
        }
        size++;
        this.notify();
    }
    int take() throws InterruptedException {
        //判断是否为空，为空时进入阻塞
        //判空时需要加锁，保证数据真实有效
        synchronized (this) {
            while(size == 0) {
               wait();
            }
        }
        int ret = array[head];
        head++;
        if(head >= array.length) {
            head = 0;
        }
        size--;
        this.notify();
        return ret;
    }
}
